/**
 * @author Arian Stolwijk
 */


var fadeAnim = new Class({
	
	Implements: Options,
	
	options: {
		duration: 250,
		max: 0.8,
		min: 0.2
	},
	
	initialize: function(elmt,options){		
		this.elmt = document.id(elmt);
		this.setOptions(options);
		this.elmt.set('tween',{duration:this.options.duration});
		this.fase = 'in';
	},
	
	start:function(){
		this.timer = this.animate.periodical(this.options.duration,this);
	},
	
	stop: function(defaultValue){
		$clear(this.timer);
		this.elmt.get('tween').cancel();
		if($defined(defaultValue)){
			this.elmt.fade(defaultValue);
		}		
	},
	
	animate: function(){
		if(this.fase == 'in'){
			this.elmt.fade(this.options.min);
			this.fase = 'out';
		}else{
			this.elmt.fade(this.options.max);
			this.fase = 'in';
		}
	},
	
	toElement: function(){
		return this.elmt;
	}
	
});

Element.Properties.fadeAnim  = {
	
	set: function(options){
		var fadeAnim = this.retrieve('fadeAnim');
		if (fadeAnim) fadeAnim.cancel();
		return this.eliminate('fadeAnim').store('fadeAnim:options',options);
	},
 
	get: function(options){
		if (options || !this.retrieve('fadeAnim')){
			if (options || !this.retrieve('fadeAnim:options')) this.set('fadeAnim', options);
			this.store('fadeAnim', new fadeAnim(this, this.retrieve('fadeAnim:options')));
		}
		return this.retrieve('fadeAnim');
	}
};

Element.implement('fadeAnim',function(options){
	this.get('fadeAnim').start();
	return this;
});
